package com.tju.resource.oauth.controller.tb;

import com.tju.resource.annotation.Log;
import com.tju.resource.model.TbConsumables;
import com.tju.resource.business.tb_consumables.TbConsumablesService;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.result.SimpleXls;
import com.tju.resource.tool.enum_utils.FunctionType;
import com.tju.resource.tool.tool.ClientParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
public class TbConsumablesController {
    private final String prefix = "consumables";

    @Autowired
    TbConsumablesService tbConsumablesService;

    /**
     * 查询耗材
     *
     * @param parameters
     * @return
     */
    @PreAuthorize("hasAuthority('sys:matl:consumables:list')")
    @PostMapping(prefix + "/getList")
    public Result<Object> getList(@RequestBody ClientParameters parameters) {
        return tbConsumablesService.getList(parameters);
    }

    /**
     * 添加耗材
     *
     * @param tbConsumables
     * @return
     */
    @PreAuthorize("hasAuthority('sys:matl:consumables:add')")
    @PostMapping(prefix + "/add")
    @Log(type= FunctionType.Add)
    public Result<Object> add(@Valid @RequestBody TbConsumables tbConsumables, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return tbConsumablesService.add(tbConsumables);
    }

    /**
     * 删除耗材
     *
     * @param tbConsumables
     * @return
     */
    @PreAuthorize("hasAuthority('sys:matl:consumables:delete')")
    @PostMapping(prefix + "/del")
    @Log(type= FunctionType.Delete)
    public Result<Object> del(@RequestBody TbConsumables tbConsumables) {
        return tbConsumablesService.del(tbConsumables);
    }

    /**
     * 修改耗材
     *
     * @param tbConsumables
     * @return
     */
    @Log(type= FunctionType.Modify)
    @PreAuthorize("hasAuthority('sys:matl:consumables:import')")
    @PostMapping(prefix + "/upd")
    public Result<Object> upd(@Valid @RequestBody TbConsumables tbConsumables, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return tbConsumablesService.upd(tbConsumables);
    }
    public static final Map<String, String> keyToBean = new HashMap<>() {{
        put("耗材名称", "name");
        put("规格", "gauge");
        put("用途", "road");
        put("数量", "number");
    }};
    /**
     * 导入耗材
     *
     * @param file
     * @return
     */
    @Log(type= FunctionType.Import)
    @PreAuthorize("hasAuthority('sys:goods:consumables:import')")
    @PostMapping(prefix + "/import")
    public Result<Object> consumablesImport(@RequestParam Map<String, String> map,@RequestParam("file") MultipartFile file) {
        try {
            return SimpleXls.saveAsBeans(
                    TbConsumables.class
                   , keyToBean
                   , file.getInputStream()
                   , tbConsumablesService::add,tbConsumables -> tbConsumables.setCreateBy(map.get("createBy")));
        } catch (IOException e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }

    }

}
